package menu.sys

import com.google.gson.*
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.junit.Test
import java.io.File

/**
 * @author futao
 * Created on 2017/12/6 - 18:54.
 */
class Test {
    @Test
    fun testCase002() {
        var gson = GsonBuilder().setPrettyPrinting().create()
        val readFileToString = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\menu.json"))).asJsonObject

        val role = JsonObject()

        readFileToString.entrySet().forEach { it ->
            println(it.key)
            it.value.asJsonObject
        }
        readFileToString.entrySet().forEach { (t, u) ->
            println(t)
            val u = u as JsonObject
            //action code is hear
            val role_u = JsonObject()
            role.add(t, role_u)
            //over
            if (u.has("menuMap")) {
                val role_menuMap = JsonObject()
                role_u.add("menuMap", role_menuMap)
                u.getAsJsonObject("menuMap").entrySet().forEach { (t2, u2) ->
                    val u2 = u2 as JsonObject
                    val role_u2 = JsonObject()
                    role_menuMap.add(t2, role_u2)
                    println("\t${t2}")

                    //action code is hear
                    if (u2.has("ctrl")) {
                        var ctrl = u2.get("ctrl") as JsonObject
                        if (ctrl.has("auths")) {
                            val role_auth = JsonArray()
                            val auths = ctrl.getAsJsonArray("auths")
                            role_u2.add("auth", role_auth)
                            auths.forEach { _auth ->
                                if (_auth !is JsonObject) return@forEach
                                val auth = _auth as JsonObject
                                println("\t\t key:${auth.get("key")} title:${auth.get("title")} val:${auth.get("val")}")
                            }
                        }
                    }
                    //over
                    if (u2.has("items")) {
                        u2.getAsJsonArray("items").forEach { u3 ->
                            val u3 = u3 as JsonObject
                            println("\t\t${u3.get("title")}")

                            //action code is hear
                            //over

                        }
                    }

                }
            }
        }
        println(StringUtils.repeat('=', 80))
        println(gson.toJson(role))
    }

    @Test
    fun test() {
        val jsonObject = JsonObject()
        jsonObject.add("aa", Gson().toJsonTree("false"))
        println(jsonObject)
    }
}